package com.qf.flash.mvp.presenter;

import com.hyphenate.easeui.utils.ChatUserCacheMgr;
import com.qf.dblib.dao.entity.ChatUser;
import com.qf.dblib.dao.entity.ChatUserBean;
import com.qf.dblib.dao.impl.UserMgr;
import com.qf.flash.http.HttpManager;
import com.qf.flash.mvp.contract.ContractListContrat;
import com.qf.flashlib.base.BasePresenter;
import com.qf.flashlib.base.BaseSubscriber;
import com.qf.flashlib.util.RxUtils;

import org.litepal.LitePal;

import java.util.List;

public class ContatListPresenter extends BasePresenter<ContractListContrat.View> implements ContractListContrat.Presenter {
    @Override
    public void getFriendList(int pageNo, int pageSize) {
        this.getFriendList("", pageNo, pageSize);
    }

    @Override
    public void getFriendList(String orderId, int pageNo, int pageSize) {
        String uid = UserMgr.getInstance().getLoginUser().getUid();
        addSubscription(HttpManager.getInstance().getGsonHttpApi()
                .GET_FRIEND_LIST(uid,orderId,pageNo,pageSize)
                .compose(RxUtils.handleResult())
                .compose(RxUtils.io_main())
                .subscribeWith(new BaseSubscriber<List<ChatUserBean>>(mRootView) {
                                   @Override
                                   public void onSuccess(List<ChatUserBean> chatUserBeans) {
                                       LitePal.saveAll(chatUserBeans);
                                       mRootView.setUi(chatUserBeans);

                                       for (ChatUserBean chatUserBean : chatUserBeans) {
                                           ChatUser user = new ChatUser();
                                           user.setUid(chatUserBean.getUid());
                                           user.setAvatar(chatUserBean.getPhoto());
                                           user.setUsername(chatUserBean.getName());
                                           user.setNick(chatUserBean.getName());
                                           ChatUserCacheMgr.getInstance().saveUser(user);
                                       }
                                   }
                               }
                )
        );
    }
}
